package com.blackfish.server;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.blackfish.server.job.UdpConf;
import com.blackfish.server.job.UdpJob;
import org.tio.server.TioServer;
import org.tio.server.ServerGroupContext;
import org.tio.server.intf.ServerAioHandler;
import org.tio.server.intf.ServerAioListener;
import org.tio.utils.quartz.QuartzUtils;

/**
 *
 * @author tanyaowu
 * 2017年3月27日 上午12:16:31
 */
public class ShowcaseServerStarter {
	static ServerAioHandler aioHandler = new ShowcaseServerAioHandler();
	static ServerAioListener aioListener = new ShowcaseServerAioListener();
	static ServerGroupContext serverGroupContext = new ServerGroupContext(aioHandler, aioListener);
	static TioServer tioServer = new TioServer(serverGroupContext); //可以为空

	static String serverIp = "127.0.0.1";
	static int serverPort = com.blackfish.common.Const.PORT;
	static UdpConf udpConf = new UdpConf();

	public static boolean isboolIp(String ipAddress)
	{
		if (ipAddress.length() < 7 || ipAddress.length() > 15) {
			return false;
		}
		String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
		Pattern pattern = Pattern.compile(ip);
		Matcher matcher = pattern.matcher(ipAddress);
		return matcher.matches();
	}

	public static void main(String[] args) throws IOException {
		if(isboolIp(args[0])){
			serverIp = args[0];
			udpConf.setServerip(args[0]);
		}

		tioServer.start(serverIp, serverPort);
		QuartzUtils.start();
	}
}